<template>
  <XtxDialog
    :title="(form.id ? '编辑' : '添加') + '收货地址'"
    v-model="dialogVisible"
  >
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input
              v-model="form.receiver"
              class="input"
              placeholder="请输入收货人"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input
              v-model="form.contact"
              class="input"
              placeholder="请输入手机号"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity
              placeholder="请选择所在地区"
              :fullLocation="form.fullLocation"
              @change="changeCty"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input
              v-model="form.address"
              class="input"
              placeholder="请输入详细地址"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input
              v-model="form.postalCode"
              class="input"
              placeholder="请输入邮政编码"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input
              v-model="form.addressTags"
              class="input"
              placeholder="请输入地址标签，逗号分隔"
            />
          </div>
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton
        @click="dialogVisible = false"
        type="gray"
        style="margin-right: 20px;"
      >
        取消
      </XtxButton>
      <XtxButton @click="submit" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { reactive, ref } from 'vue'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup(props, { emit }) {
    const dialogVisible = ref(false)
    // 打开函数
    const open = (address) => {
      dialogVisible.value = true
      console.log('address', address)
      if (!address.id) {
        for (let k in form) {
          if (k === 'isDefault') {
            form[k] = 0
          } else {
            form[k] = null
          }
        }
      } else {
        for (let k in address) {
          form[k] = address[k]
        }
      }
    }

    // 表单数据
    const form = reactive({
      id: '',
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 0,
      fullLocation: '',
    })

    // 选择地区
    const changeCty = (data) => {
      form.provinceCode = data.provinceCode
      form.cityCode = data.cityCode
      form.countyCode = data.countyCode
      form.fullLocation = data.fullLocation
    }

    // 提交表单操作
    const submit = async () => {
      if (form.id) {
        await editAddress(form)
        Message({ msg: '修改收货地址成功', type: 'success' })
      } else {
        addAddress(form)
          .then((data) => {
            form.id = data.result.id
            // 添加成功
            Message({ msg: '添加收货地址成功', type: 'success' })
          })
          .catch((e) => {
            Message({ msg: e.response.data.message, type: 'error' })
          })
      }
      dialogVisible.value = false
      emit('on-success', form)
    }

    return { dialogVisible, open, form, changeCty, submit }
  },
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
